#include "mainwidget.h"
#include "ui_mainwidget.h"
#include <QDebug>
MainWidget::MainWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MainWidget)
{
    ui->setupUi(this);
    this->resize(640,800);
    label = new QLabel("黄箴语",this);

   timerID1 = this->startTimer(1000);
   timerID2 = this->startTimer(2000);
}

MainWidget::~MainWidget()
{
    delete ui;
}

void MainWidget::mousePressEvent(QMouseEvent *event)
{
    qDebug()<<"鼠标按下";
}

void MainWidget::mouseReleaseEvent(QMouseEvent *event)
{
     qDebug()<<"鼠标释放";
}

void MainWidget::mouseDoubleClickEvent(QMouseEvent *event)
{
    qDebug()<<"鼠标双击";
}

void MainWidget::mouseMoveEvent(QMouseEvent *event)
{
//                            全局                  相对
    qDebug()<<"移动 "<<event->globalPos()<<":::"<<event->pos();
     label->move(event->pos());
}

void MainWidget::keyPressEvent(QKeyEvent *event)
{
    qDebug()<<event->key()<<"按下";
    if(event->key()==68)
    {
        label->move(300,400);
    }
}

void MainWidget::keyReleaseEvent(QKeyEvent *event)
{

    qDebug()<<event->key()<<"释放";
}

void MainWidget::paintEvent(QPaintEvent *event)
{
    qDebug()<<"绘图事件";
}

void MainWidget::timerEvent(QTimerEvent *event)
{
    static int time1 =0;
    static int time2 =0;
    if(event->timerId() == timerID1)
    {
        qDebug()<<"定时器事件1"<<time1++;
    }
    if(event->timerId() == timerID2)
    {
        qDebug()<<"定时器事件2"<<time2++;
    }

}

bool MainWidget::event(QEvent *event)
{
    //分发事件 - 事件被处理返回true 否则返回false
//    事件过滤
    if(event->type()==QEvent::MouseButtonPress)
    {
        qDebug()<<"自定义QEvent::MouseButtonPress";
        return false;
    }
    else if(event->type() == QEvent::MouseMove)
    {
        QMouseEvent *mouseEvent = dynamic_cast<QMouseEvent*>(event);
        mouseMoveEvent(mouseEvent);
    }
    //对于不关心的事件交给基类分发
        return QWidget::event(event);
}
